package com.pkk.spring.cloud.config.nacos.properties;

import com.pkk.spring.cloud.config.nacos.properties.NacosProperties.NacosConfigProperties;
import java.util.HashMap;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Import;

/**
 * 动态路由的配置
 *
 * @author peikunkun
 * @version V1.0
 * @date 2021-01-23 17:14
 **/
@Getter
@Setter
@ConditionalOnProperty(name = "penguin.cloud.gateway", matchIfMissing = true)
@ConfigurationProperties(prefix = "penguin.cloud.gateway")
@Import(NacosConfigProperties.class)
public class NacosProperties {

  /**
   * 动态配置
   */
  private Map<String, NacosConfigProperties> dynamic = new HashMap<String, NacosConfigProperties>();


  /**
   * 此用于填充默认值
   */
  private static String DEFAULT_SERVER;
  private static String DEFAULT_NAMESPACE;
  private static String DEFAULT_GROUP;
  private static String DEFAULT_DATA_ID;


  @Value("${spring.cloud.nacos.config.server-addr:'http://127.0.0.1:8848'}")
  public void setDefaultServer(String defaultServer) {
    DEFAULT_SERVER = defaultServer;
  }

  @Value("${spring.cloud.nacos.config.namespace:'dev'}")
  public void setDefaultNamespace(String defaultNamespace) {
    DEFAULT_NAMESPACE = defaultNamespace;
  }

  @Value("${spring.cloud.nacos.config.group:'DEFAULT_GROUP1'}")
  public void setDefaultGroup(String defaultGroup) {
    DEFAULT_GROUP = defaultGroup;
  }

  @Value("${spring.cloud.nacos.config.service:${spring.application.name}}")
  public void setDefaultDataId(String defaultDataId) {
    DEFAULT_DATA_ID = defaultDataId;
  }

  /**
   * nacos的配置
   *
   * @author peikunkun
   * @date 2021/1/26 0026 下午 4:00
   * @since
   */
  @Getter
  @Setter
  @NoArgsConstructor
  @AllArgsConstructor
  public static class NacosConfigProperties {


    /**
     *
     */
    private boolean enable = true;


    /**
     * 动态路由获取的配置地址
     */
    private String serverAddr;

    /**
     * 命名空间
     */
    private String namespace;

    /**
     * 分组
     */
    private String group;

    /**
     * 数据id
     */
    private String dataId;


    public String getServerAddr() {
      if (StringUtils.isBlank(serverAddr)) {
        this.serverAddr = DEFAULT_SERVER;
      }
      return serverAddr;
    }

    public String getNamespace() {
      if (StringUtils.isBlank(namespace)) {
        this.namespace = DEFAULT_NAMESPACE;
      }
      return namespace;
    }

    public String getGroup() {
      if (StringUtils.isBlank(group)) {
        this.group = DEFAULT_GROUP;
      }
      return group;
    }


    public String getDataId() {
      if (StringUtils.isBlank(dataId)) {
        this.dataId = DEFAULT_DATA_ID;
      }
      if (StringUtils.isBlank(this.dataId)) {
        throw new RuntimeException("未知的路由配置,请指定配置[penguin.cloud.gateway.dynamic.$.dataId]");
      }
      return dataId;
    }
  }


}
